package cn.iocoder.yudao.module.agent.service.brokerageclue;

import cn.iocoder.yudao.module.agent.dal.dataobject.user.AgentUserDO;
import cn.iocoder.yudao.module.agent.service.user.AgentUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.agent.controller.admin.brokerageclue.vo.*;
import cn.iocoder.yudao.module.agent.dal.dataobject.brokerageclue.AgentBrokerageClueDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.agent.convert.brokerageclue.AgentBrokerageClueConvert;
import cn.iocoder.yudao.module.agent.dal.mysql.brokerageclue.AgentBrokerageClueMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.agent.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 代理人分销用户线索 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class AgentBrokerageClueServiceImpl implements AgentBrokerageClueService {

    @Resource
    private AgentBrokerageClueMapper brokerageClueMapper;

    @Resource
    private AgentUserService agentUserService;

    @Override
    public Long createBrokerageClue(AgentBrokerageClueCreateReqVO createReqVO) {

        AgentUserDO agentUser = agentUserService.getByUserId(createReqVO.getMemberId());
        if(agentUser == null){
            throw exception(BROKERAGE_CLUE_ADD_INVALID);
        }

        AgentBrokerageClueDO clueDO = getBrokerageClueByAccount(createReqVO.getWxAccount());
        if(clueDO != null){
            throw exception(BROKERAGE_CLUE_HAD_EXISTS);
        }

        // 插入
        AgentBrokerageClueDO brokerageClue = AgentBrokerageClueConvert.INSTANCE.convert(createReqVO);
        brokerageClueMapper.insert(brokerageClue);
        // 返回
        return brokerageClue.getId();
    }

    @Override
    public void updateBrokerageClue(AgentBrokerageClueUpdateReqVO updateReqVO) {
        // 校验存在
        validateBrokerageClueExists(updateReqVO.getId());
        // 更新
        AgentBrokerageClueDO updateObj = AgentBrokerageClueConvert.INSTANCE.convert(updateReqVO);
        brokerageClueMapper.updateById(updateObj);
    }

    @Override
    public void deleteBrokerageClue(Long id) {
        // 校验存在
        validateBrokerageClueExists(id);
        // 删除
        brokerageClueMapper.deleteById(id);
    }

    private void validateBrokerageClueExists(Long id) {
        if (brokerageClueMapper.selectById(id) == null) {
            throw exception(BROKERAGE_CLUE_NOT_EXISTS);
        }
    }

    @Override
    public AgentBrokerageClueDO getBrokerageClue(Long id) {
        return brokerageClueMapper.selectById(id);
    }

    @Override
    public AgentBrokerageClueDO getBrokerageClueByAccount(String account){
        return brokerageClueMapper.selectOne(AgentBrokerageClueDO::getWxAccount, account);
    }

    @Override
    public List<AgentBrokerageClueDO> getBrokerageClueList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return brokerageClueMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AgentBrokerageClueDO> getBrokerageCluePage(AgentBrokerageCluePageReqVO pageReqVO) {
        return brokerageClueMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AgentBrokerageClueDO> getBrokerageClueList(AgentBrokerageClueExportReqVO exportReqVO) {
        return brokerageClueMapper.selectList(exportReqVO);
    }

}
